﻿using System;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Collections;
using System.Collections.Generic;
using System.Data.SqlClient;


public partial class Main_Utilities_UserPermission : System.Web.UI.Page
{
    Datautility DU = new Datautility();
    string strsql = "";
    DataTable DTMain = new DataTable();
    List<int> LstModid=new List<int>();
    //List<int> LstRight = new List<int>();
    List<int> LstNEWRight = new List<int>();
    List<int> LstADDRight = new List<int>();
    List<int> LstEDITRight = new List<int>(); 
    List<int> LstDELETERight = new List<int>();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        { 
            BindDropdownList();
            BindData();
        }
    }
    protected void BindDropdownList()
    {
        //strsql = "select UserId, FirstName+' [ '+UserName+' ]' as UserName from login where GroupId>='0'";
        strsql = "Select UserId,FirstName+' [ '+Username+' ]' as UserName from Login where isgroupactive='0'";
        DataTable DT = DU.getdata(strsql);
        if (DT.Rows.Count > 0)
        {
            ddlUser.DataSource = DT;
            ddlUser.DataTextField = "UserName";
            ddlUser.DataValueField = "UserId";
            ddlUser.DataBind();
            ddlUser.Items.Insert(0, new ListItem("--Select User --", "0"));
            ddlUser.SelectedValue = "0";
        }
    }   
    protected void ddlUser_SelectIndexChanged(object sender, EventArgs e)
    {
        if (ddlUser.SelectedValue == "1")
        {            
            panelMain.Enabled = false;
        }
        else
        {
            panelMain.Enabled = true;
        }
        BindData();
    }    
    protected void BindData()
    {
        DTMain=DU.getdatausingprocedure("USP_ACC_GetAllSecurityModules");
        DataTable DTAccount = (DataTable)DTMain;
        if (DTAccount.Rows.Count > 0) 
        {
            DTAccount.DefaultView.RowFilter = "PROJECTTYPE='0'";
            DTAccount = DTAccount.DefaultView.ToTable(true, "Modules");
            DTAccount.DefaultView.Sort = "Modules ASC";
            datalistAccount.DataSource = DTAccount;
            datalistAccount.DataBind();
        }
        DataTable DTInvoice = (DataTable)DTMain;
        if (DTInvoice.Rows.Count > 0)
        {
            DTInvoice.DefaultView.RowFilter = "PROJECTTYPE='1'";
            DTInvoice = DTInvoice.DefaultView.ToTable(true, "Modules");
            DTInvoice.DefaultView.Sort = "Modules ASC";
            datalistInvoice.DataSource = DTInvoice;
            datalistInvoice.DataBind();
        }
        DataTable DTInventory = (DataTable)DTMain;
        if (DTInventory.Rows.Count > 0)
        {
            DTInventory.DefaultView.RowFilter = "PROJECTTYPE='2'";
            DTInventory = DTInventory.DefaultView.ToTable(true, "Modules");
            DTInventory.DefaultView.Sort = "Modules ASC";
            datalistInventory.DataSource = DTInventory;
            datalistInventory.DataBind();
        }
        DataTable DTGeneral= (DataTable)DTMain;
        if (DTGeneral.Rows.Count > 0)
        {
            DTGeneral.DefaultView.RowFilter = "PROJECTTYPE='3'";
            DTGeneral = DTGeneral.DefaultView.ToTable(true, "Modules");
            DTGeneral.DefaultView.Sort = "Modules ASC";
            datalistGeneral.DataSource = DTGeneral;
            datalistGeneral.DataBind();
        }
    }
    protected void datalistAccount_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Label lblHeading = (Label)e.Item.FindControl("lblHeading");
            GridView grdAccount = (GridView)e.Item.FindControl("grdAccount");
            
            DataTable DT = (DataTable)DTMain;
            //if (DT.Rows[e.Item.ItemIndex]["ISACTIVE"].ToString() == "False")
            //{
            //    e.Item.Enabled = false;
            //}
            DT.DefaultView.RowFilter = "PROJECTTYPE='0'";
            if (DT.Rows.Count > 0)
            {                
                DataView DView = DT.DefaultView;
                DView.RowFilter = "Modules='" + lblHeading.Text.Trim() + "'and PROJECTTYPE='0'";
                DataTable DT1 = DView.ToTable();

                grdAccount.DataSource = DT1;
                grdAccount.DataBind();
            }
        }
    }
    protected void datalistInvoice_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Label lblHeading = (Label)e.Item.FindControl("lblHeading");
            GridView grdInvoice = (GridView)e.Item.FindControl("grdInvoice");
            DataTable DT = (DataTable)DTMain;
            //if (DT.Rows[e.Item.ItemIndex]["ISACTIVE"].ToString() == "False")
            //{
            //    e.Item.Enabled = false;
            //}
            DT.DefaultView.RowFilter = "PROJECTTYPE='1'";
            if (DT.Rows.Count > 0)
            {
                DataView DView = DT.DefaultView;
                DView.RowFilter = "Modules='" + lblHeading.Text.Trim() + "'and PROJECTTYPE='1'";
                DataTable DT1 = DView.ToTable();
                grdInvoice.DataSource = DT1;
                grdInvoice.DataBind();
            }
        }
    }
    protected void datalistInventory_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Label lblHeading = (Label)e.Item.FindControl("lblHeading");
            GridView grdInventory = (GridView)e.Item.FindControl("grdInventory");
            DataTable DT = (DataTable)DTMain;
            //if (DT.Rows[e.Item.ItemIndex]["ISACTIVE"].ToString() == "False")
            //{
            //    e.Item.Enabled = false;
            //}
            DT.DefaultView.RowFilter = "PROJECTTYPE='2'";
            if (DT.Rows.Count > 0)
            {
                DataView DView = DT.DefaultView;
                DView.RowFilter = "Modules='" + lblHeading.Text.Trim() + "'and PROJECTTYPE='2'";
                DataTable DT1 = DView.ToTable();
                grdInventory.DataSource = DT1;
                grdInventory.DataBind();
            }
        }
    }
    protected void datalistGeneral_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            Label lblHeading = (Label)e.Item.FindControl("lblHeading");
            GridView grdGeneral = (GridView)e.Item.FindControl("grdGeneral");
            DataTable DT = (DataTable)DTMain;
            //if (DT.Rows[e.Item.ItemIndex]["ISACTIVE"].ToString() == "False")
            //{
            //    e.Item.Enabled = false;
            //}
            DT.DefaultView.RowFilter = "PROJECTTYPE='3'";
            if (DT.Rows.Count > 0)
            {
                DataView DView = DT.DefaultView;
                DView.RowFilter = "Modules='" + lblHeading.Text.Trim() + "'and PROJECTTYPE='3'";
                DataTable DT1 = DView.ToTable();
                grdGeneral.DataSource = DT1;
                grdGeneral.DataBind();
            }
        }
    }
    protected void grdBind_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        GridView grdBind = (GridView)sender;
        DataListItem dlItem = (DataListItem)grdBind.Parent;
        Label lblhed = (Label)dlItem.FindControl("lblHeading");
        DataTable DT4 = (DataTable)DTMain;        
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DataView dv = DT4.DefaultView;
            dv.RowFilter = "Modules='" + lblhed.Text + "'";
            DataTable dt6 = dv.ToTable();
            if (dt6.Rows[e.Row.RowIndex]["ISACTIVE"].ToString() == "False")
            {
                e.Row.Enabled = false;
                e.Row.BackColor = System.Drawing.Color.LightGray;
            }
            string dk = grdBind.DataKeys[e.Row.RowIndex].Value.ToString();
            strsql = "select * from ACC_SECUSERRULES where userid='" + ddlUser.SelectedValue + "' and modid='" + dk.ToString() + "'";
            DataTable DT = DU.getdata(strsql);
            if (DT.Rows.Count > 0)
            {
                CheckBox chkMain = (CheckBox)e.Row.Cells[0].FindControl("chkMain");
                CheckBox chkNew = (CheckBox)e.Row.Cells[0].FindControl("chkNew");
                CheckBox chkEdit = (CheckBox)e.Row.Cells[0].FindControl("chkEdit");
                CheckBox chkDelete = (CheckBox)e.Row.Cells[0].FindControl("chkDelete");
                CheckBox chkPrint = (CheckBox)e.Row.Cells[0].FindControl("chkPrint");
                chkMain.Checked = true;
                if (Convert.ToDouble(DT.Rows[0]["RIGHTS"].ToString()) == 62)
                {
                    chkNew.Checked = true;
                    chkEdit.Checked = true;
                    chkDelete.Checked = true;
                    chkPrint.Checked = true;
                }
                else if (Convert.ToDouble(DT.Rows[0]["RIGHTS"].ToString()) == 4)
                {
                    chkNew.Checked = true;                    
                }
                else if (Convert.ToDouble(DT.Rows[0]["RIGHTS"].ToString()) == 6)
                {                  
                    chkEdit.Checked = true;                    
                }
                else if (Convert.ToDouble(DT.Rows[0]["RIGHTS"].ToString()) == 18)
                {                   
                    chkDelete.Checked = true;                    
                }
                else if (Convert.ToDouble(DT.Rows[0]["RIGHTS"].ToString()) == 34)
                {                   
                    chkPrint.Checked = true;
                }
                else if (Convert.ToDouble(DT.Rows[0]["RIGHTS"].ToString()) == 10)
                {
                    chkNew.Checked = true;
                    chkEdit.Checked = true;                   
                }
                else if (Convert.ToDouble(DT.Rows[0]["RIGHTS"].ToString()) == 22)
                {
                    chkNew.Checked = true;                    
                    chkDelete.Checked = true;                    
                }
                else if (Convert.ToDouble(DT.Rows[0]["RIGHTS"].ToString()) == 38)
                {
                    chkNew.Checked = true;                   
                    chkPrint.Checked = true;
                }
                else if (Convert.ToDouble(DT.Rows[0]["RIGHTS"].ToString()) == 24)
                {
                    chkEdit.Checked = true;
                    chkDelete.Checked = true;
                }
                else if (Convert.ToDouble(DT.Rows[0]["RIGHTS"].ToString()) == 40)
                {
                    chkEdit.Checked = true;
                    chkPrint.Checked = true;
                }
                else if (Convert.ToDouble(DT.Rows[0]["RIGHTS"].ToString()) == 52)
                {
                    chkDelete.Checked = true;
                    chkPrint.Checked = true;
                }
                else
                {
                    chkNew.Checked = false;
                    chkEdit.Checked = false;
                    chkDelete.Checked = false;
                    chkPrint.Checked = false;
                }                

            }
            ((CheckBox)e.Row.FindControl("chkMain")).Attributes.Add("OnClick", "getChecked('" + ((CheckBox)e.Row.FindControl("chkMain")).ClientID+"','" +
            ((CheckBox)e.Row.FindControl("chkNew")).ClientID + "','" + ((CheckBox)e.Row.FindControl("chkEdit")).ClientID
            + "','" + ((CheckBox)e.Row.FindControl("chkDelete")).ClientID + "','" + ((CheckBox)e.Row.FindControl("chkPrint")).ClientID + "'," + "'All')");

            ((CheckBox)e.Row.FindControl("chkNew")).Attributes.Add("OnClick", "getChecked('" + ((CheckBox)e.Row.FindControl("chkMain")).ClientID + "','" +
            ((CheckBox)e.Row.FindControl("chkNew")).ClientID + "','" + ((CheckBox)e.Row.FindControl("chkEdit")).ClientID
            + "','" + ((CheckBox)e.Row.FindControl("chkDelete")).ClientID + "','" + ((CheckBox)e.Row.FindControl("chkPrint")).ClientID + "'," + "'New')");


            ((CheckBox)e.Row.FindControl("chkEdit")).Attributes.Add("OnClick", "getChecked('" + ((CheckBox)e.Row.FindControl("chkMain")).ClientID + "','" +
            ((CheckBox)e.Row.FindControl("chkNew")).ClientID + "','" + ((CheckBox)e.Row.FindControl("chkEdit")).ClientID
            + "','" + ((CheckBox)e.Row.FindControl("chkDelete")).ClientID + "','" + ((CheckBox)e.Row.FindControl("chkPrint")).ClientID + "'," + "'Edit')");



            ((CheckBox)e.Row.FindControl("chkDelete")).Attributes.Add("OnClick", "getChecked('" + ((CheckBox)e.Row.FindControl("chkMain")).ClientID + "','" +
            ((CheckBox)e.Row.FindControl("chkNew")).ClientID + "','" + ((CheckBox)e.Row.FindControl("chkEdit")).ClientID
            + "','" + ((CheckBox)e.Row.FindControl("chkDelete")).ClientID + "','" + ((CheckBox)e.Row.FindControl("chkPrint")).ClientID + "'," + "'Delete')");



            ((CheckBox)e.Row.FindControl("chkPrint")).Attributes.Add("OnClick", "getChecked('" + ((CheckBox)e.Row.FindControl("chkMain")).ClientID + "','" +
            ((CheckBox)e.Row.FindControl("chkNew")).ClientID + "','" + ((CheckBox)e.Row.FindControl("chkEdit")).ClientID
            + "','" + ((CheckBox)e.Row.FindControl("chkDelete")).ClientID + "','" + ((CheckBox)e.Row.FindControl("chkPrint")).ClientID + "'," + "'Print')");

        }
    }
    protected void GetCheckedbox()
    {   
        for (int i = 0; i < datalistAccount.Items.Count; i++)
        {
            GridView grdAccount = (GridView)datalistAccount.Items[i].FindControl("grdAccount");
            for (int j = 0; j < grdAccount.Rows.Count; j++)
            {
                CheckBox chkMain = (CheckBox)grdAccount.Rows[j].Cells[0].FindControl("chkMain");
                CheckBox chkNew = (CheckBox)grdAccount.Rows[j].Cells[0].FindControl("chkNew");
                CheckBox chkEdit = (CheckBox)grdAccount.Rows[j].Cells[0].FindControl("chkEdit");
                CheckBox chkDelete = (CheckBox)grdAccount.Rows[j].Cells[0].FindControl("chkDelete");
                CheckBox chkPrint = (CheckBox)grdAccount.Rows[j].Cells[0].FindControl("chkPrint");
                if (chkMain.Checked == true)
                {
                    int newright = 0; int editright = 0; int deleteright = 0; int addright = 0;
                    int dk =Convert.ToInt32(grdAccount.DataKeys[j].Value.ToString());
                    LstModid.Add(dk);
                    if (chkNew.Checked == true)
                    {
                        newright += 1;
                    }
                    if (chkEdit.Checked == true)
                    {
                        editright += 1;
                    }
                    if (chkDelete.Checked == true)
                    {
                        deleteright += 1;
                    }
                    if (chkPrint.Checked == true)
                    {
                        addright += 1;
                    }
                    LstNEWRight.Add(newright);
                    LstADDRight.Add(addright);
                    LstDELETERight.Add(deleteright);
                    LstEDITRight.Add(editright);                 
                }
            }
        }

        for (int i = 0; i < datalistInvoice.Items.Count; i++)
        {
            GridView grdInvoice = (GridView)datalistInvoice.Items[i].FindControl("grdInvoice");
            for (int j = 0; j < grdInvoice.Rows.Count; j++)
            {
                CheckBox chkMain = (CheckBox)grdInvoice.Rows[j].Cells[0].FindControl("chkMain");
                CheckBox chkNew = (CheckBox)grdInvoice.Rows[j].Cells[0].FindControl("chkNew");
                CheckBox chkEdit = (CheckBox)grdInvoice.Rows[j].Cells[0].FindControl("chkEdit");
                CheckBox chkDelete = (CheckBox)grdInvoice.Rows[j].Cells[0].FindControl("chkDelete");
                CheckBox chkPrint = (CheckBox)grdInvoice.Rows[j].Cells[0].FindControl("chkPrint");
                if (chkMain.Checked == true)
                {
                    int newright = 0; int editright = 0; int deleteright = 0; int addright = 0;
                    int dk = Convert.ToInt32(grdInvoice.DataKeys[j].Value.ToString());
                    LstModid.Add(dk);
                    if (chkNew.Checked == true)
                    {
                        newright += 1;
                    }
                    if (chkEdit.Checked == true)
                    {
                        editright += 1;
                    }
                    if (chkDelete.Checked == true)
                    {
                        deleteright += 1;
                    }
                    if (chkPrint.Checked == true)
                    {
                        addright += 1;
                    }
                    LstNEWRight.Add(newright);
                    LstADDRight.Add(addright);
                    LstDELETERight.Add(deleteright);
                    LstEDITRight.Add(editright);      
                }
            }
        }
        for (int i = 0; i < datalistInventory.Items.Count; i++)
        {
            GridView grdInventory = (GridView)datalistInventory.Items[i].FindControl("grdInventory");
            for (int j = 0; j < grdInventory.Rows.Count; j++)
            {
                CheckBox chkMain = (CheckBox)grdInventory.Rows[j].Cells[0].FindControl("chkMain");
                CheckBox chkNew = (CheckBox)grdInventory.Rows[j].Cells[0].FindControl("chkNew");
                CheckBox chkEdit = (CheckBox)grdInventory.Rows[j].Cells[0].FindControl("chkEdit");
                CheckBox chkDelete = (CheckBox)grdInventory.Rows[j].Cells[0].FindControl("chkDelete");
                CheckBox chkPrint = (CheckBox)grdInventory.Rows[j].Cells[0].FindControl("chkPrint");
                if (chkMain.Checked == true)
                {
                    int newright = 0; int editright = 0; int deleteright = 0; int addright = 0;
                    int dk = Convert.ToInt32(grdInventory.DataKeys[j].Value.ToString());
                    LstModid.Add(dk);
                    if (chkNew.Checked == true)
                    {
                        newright += 1;
                    }
                    if (chkEdit.Checked == true)
                    {
                        editright += 1;
                    }
                    if (chkDelete.Checked == true)
                    {
                        deleteright += 1;
                    }
                    if (chkPrint.Checked == true)
                    {
                        addright += 1;
                    }
                    LstNEWRight.Add(newright);
                    LstADDRight.Add(addright);
                    LstDELETERight.Add(deleteright);
                    LstEDITRight.Add(editright);   
                }
            }
        }
        for (int i = 0; i < datalistGeneral.Items.Count; i++)
        {
            GridView grdGeneral = (GridView)datalistGeneral.Items[i].FindControl("grdGeneral");
            for (int j = 0; j < grdGeneral.Rows.Count; j++)
            {
                CheckBox chkMain = (CheckBox)grdGeneral.Rows[j].Cells[0].FindControl("chkMain");
                CheckBox chkNew = (CheckBox)grdGeneral.Rows[j].Cells[0].FindControl("chkNew");
                CheckBox chkEdit = (CheckBox)grdGeneral.Rows[j].Cells[0].FindControl("chkEdit");
                CheckBox chkDelete = (CheckBox)grdGeneral.Rows[j].Cells[0].FindControl("chkDelete");
                CheckBox chkPrint = (CheckBox)grdGeneral.Rows[j].Cells[0].FindControl("chkPrint");
                if (chkMain.Checked == true)
                {
                    int newright = 0; int editright = 0; int deleteright = 0; int addright = 0;
                    int dk = Convert.ToInt32(grdGeneral.DataKeys[j].Value.ToString());
                    LstModid.Add(dk);
                    if (chkNew.Checked == true)
                    {
                        newright += 1;
                    }
                    if (chkEdit.Checked == true)
                    {
                        editright += 1;
                    }
                    if (chkDelete.Checked == true)
                    {
                        deleteright += 1;
                    }
                    if (chkPrint.Checked == true)
                    {
                        addright += 1;
                    }
                    LstNEWRight.Add(newright);
                    LstADDRight.Add(addright);
                    LstDELETERight.Add(deleteright);
                    LstEDITRight.Add(editright);   
                }
            }
        }
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        try
        {
            GetCheckedbox();
            int count = LstModid.Count;
            if (ddlUser.SelectedValue != "0")
            {
                //strsql = "Delete from ACC_SECUSERRULES where UserId='"+ddlUser.SelectedValue+"'";
                strsql = "Delete from User_permission where User_Id='"+ddlUser.SelectedValue+"'";
                DU.executesql(strsql);
                if (count > 0)
                {
                    int flag = 0;
                        for (int i = 0; i < LstModid.Count; i++)
                        {
                            SqlParameter[] sparam =
                                            {
                                                new SqlParameter("@USERID",ddlUser.SelectedValue),
                                                new SqlParameter("@MODULEID",LstModid[i].ToString()),
                                                new SqlParameter("@PAGEADD",LstADDRight[i].ToString()),
                                                new SqlParameter("@PAGEEDIT",LstEDITRight[i].ToString()),
                                                new SqlParameter("@PAGEVIEW",LstNEWRight[i].ToString()),
                                                new SqlParameter("@PAGEDELETE",LstDELETERight[i].ToString())
                                            };
                            DU.executeprocedure("USP_ACC_SaveSecurityModuleRolesForUser", sparam);
                            flag = 1;
                        }
                        if (flag == 1)
                        {
                            hidMessage.Value = "Updated!";
                            ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "ShowAlert('" + hidMessage.Value.ToString() + "');", true);
                            //ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "ModalPopupsWait();", true);
                        }
                        else if (flag == 0)
                        {
                            hidHeading.Value = "Error:";
                            hidMessage.Value = "Error When Saving User Permission!";
                            ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "ShowAlert('" + hidMessage.Value.ToString() + "');", true);
                        }                
                    }
            
            }
            else
            {
                hidHeading.Value = "Message:";
                hidMessage.Value = "Select User for permission!";
                ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "ShowAlert('" + hidMessage.Value.ToString() + "');", true);
                ddlUser.Focus();
                return;
            }
         }
         catch (Exception ex)
         {
             hidHeading.Value = "Message:";
             hidMessage.Value = ex.Message;
             ScriptManager.RegisterStartupScript(this, this.GetType(), "script", "ShowAlert('" + hidMessage.Value.ToString() + "');", true);
             return;
         }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/Welcome.aspx");
    }
}


